import { uniGetStorageSync, uniSetStorageSync } from '../funcs';
import { RongIMLib } from './cloudUtil';
import { USER_ORIENTED } from '../config';
import { app } from '../../main';

export const CHATTER_TO = 'cloudChatTo';
export const CHATTER_FROM = 'cloudChatFrom';

export function MSG_TYPE() {
  if (RongIMLib)
    return {
      TEXT: RongIMLib.MESSAGE_TYPE.TEXT, // RC:TxtMsg
      RICH_CONTENT: RongIMLib.MESSAGE_TYPE.RICH_CONTENT, // 图文
      IMAGE: RongIMLib.MESSAGE_TYPE.IMAGE, // 图片
      AUDIO: RongIMLib.MESSAGE_TYPE.HQ_VOICE, // 语音
      HQ_VOICE: RongIMLib.MESSAGE_TYPE.HQ_VOICE, // 语音
      card: RongIMLib.MESSAGE_TYPE.TEXT, // 房源卡片之类
      FILE: RongIMLib.MESSAGE_TYPE.FILE, // 文件
      // 其他 不用的
      GIF: 'RC:GIFMsg',
      INPUTTING: 'RC:TypSts',
      // 已读
      has_read: 'RC:ReadNtf',
    };
}


/**
 * 所有聊天记录存放,
 * 使用方法: onShow()时, 从uniStorage赋值给globalData
 *        - initChatStorage,
 * onHide(), 把globalData 赋值给 uniStorage
 *        - saveChatStorage
 */
export const CHAT_STORAGE = 'CHAT_STORAGE';
export const CHAT_STORAGE_InitialStructure = {
  totalUnreadCount: 0,
  allList: [
    {
      latestMessage: {},
      chatter: null,
      myUnreadCount: 0,
    },
  ],
};


// 和saveChatStorage使用
export function initChatStorage() {
  appMain.globalData = uniGetStorageSync(CHAT_STORAGE);
}

// 和initChatStorage使用
export function saveChatStorage() {
  uniSetStorageSync(CHAT_STORAGE, getApp().gobalData.chatStorage);
}

export function senderId_(id) {
  if (id.toString().startsWith('merchant_')) return id.toString();
  if (USER_ORIENTED) {
    return id.toString();
  } else {
    return 'merchant_' + id;
  }
}


// 时分秒
export function getTime() {
  let date = new Date();
  return date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
}

export const rongEvent = {
  // 融云事件用于$emit
  chattingMessageSuccess: 'chattingMessageSuccess', // 只用于正在聊天的时候用收发
  connectSuccess: 'connectSuccess', // 聊天全局事件, 连接成功,
  globalConversationUpdated: 'globalConversationUpdated',
};
